
using System;
using QdbUnit;
using QdbUnit.Extensions;
using QdbUnit.Fixtures;
using Qdb.Config;
using Qdb.Ext;
using Qdb.Foundation;
using Qdb.Tests.Common.Config;

namespace Qdb.Tests.Common.Config
{
	public class ClassConfigOverridesGlobalConfigTestSuite : FixtureTestSuiteDescription
	{
		private static readonly FixtureVariable GlobalConfig = FixtureVariable.NewInstance
			("global");

		private static readonly FixtureVariable ClassConfig = FixtureVariable.NewInstance
			("class");

		public class ClassConfigOverridesGlobalConfigTestUnit : AbstractQdbTestCase
		{
			public class Item
			{
			}

			protected override void Configure(IConfiguration config)
			{
				config.GenerateUUIDs(((ConfigScope)GlobalConfig.Value));
				config.GenerateVersionNumbers(((ConfigScope)GlobalConfig.Value));
				if (!((TernaryBool)ClassConfig.Value).IsUnspecified())
				{
					config.ObjectClass(typeof(ClassConfigOverridesGlobalConfigTestSuite.ClassConfigOverridesGlobalConfigTestUnit.Item
						)).GenerateUUIDs(((TernaryBool)ClassConfig.Value).BooleanValue(true));
					config.ObjectClass(typeof(ClassConfigOverridesGlobalConfigTestSuite.ClassConfigOverridesGlobalConfigTestUnit.Item
						)).GenerateVersionNumbers(((TernaryBool)ClassConfig.Value).BooleanValue(true));
				}
			}

			/// <exception cref="System.Exception"></exception>
			protected override void Store()
			{
				Store(new ClassConfigOverridesGlobalConfigTestSuite.ClassConfigOverridesGlobalConfigTestUnit.Item
					());
			}

			public virtual void TestNoUUIDIsGenerated()
			{
				ClassConfigOverridesGlobalConfigTestSuite.ClassConfigOverridesGlobalConfigTestUnit.Item
					 item = ((ClassConfigOverridesGlobalConfigTestSuite.ClassConfigOverridesGlobalConfigTestUnit.Item
					)RetrieveOnlyInstance(typeof(ClassConfigOverridesGlobalConfigTestSuite.ClassConfigOverridesGlobalConfigTestUnit.Item
					)));
				IObjectInfo objectInfo = Db().Ext().GetObjectInfo(item);
				if (!((TernaryBool)ClassConfig.Value).IsUnspecified())
				{
					AssertGeneration(objectInfo, ((TernaryBool)ClassConfig.Value).BooleanValue(true) 
						&& ((ConfigScope)GlobalConfig.Value) != ConfigScope.Disabled);
				}
				else
				{
					AssertGeneration(objectInfo, ((ConfigScope)GlobalConfig.Value) == ConfigScope.Globally
						);
				}
			}

			private void AssertGeneration(IObjectInfo objectInfo, bool expectGeneration)
			{
				if (expectGeneration)
				{
					Assert.IsNotNull(objectInfo.GetUUID());
				}
				else
				{
					Assert.IsNull(objectInfo.GetUUID());
					Assert.AreEqual(0L, objectInfo.GetVersion());
				}
			}
		}

		public ClassConfigOverridesGlobalConfigTestSuite()
		{
			{
				TestUnits(new Type[] { typeof(ClassConfigOverridesGlobalConfigTestSuite.ClassConfigOverridesGlobalConfigTestUnit
					) });
				FixtureProviders(new IFixtureProvider[] { new SimpleFixtureProvider(GlobalConfig, 
					new object[] { ConfigScope.Globally, ConfigScope.Individually, ConfigScope.Disabled
					 }), new SimpleFixtureProvider(ClassConfig, new object[] { TernaryBool.Yes, TernaryBool
					.No, TernaryBool.Unspecified }) });
			}
		}
	}
}
